﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BackEnd.Facade.Interfaces;
using BackEnd.Facade.Implementations;
using WebMVC.Areas.AdminPanel.Models;

namespace WebMVC.Areas.AdminPanel.Controllers
{
    public class ReviewController : AdminControllerBase
    {
        protected IFeedbackManager _feedbackManager;
        protected IMembershipManager _membershipManager;

        public ReviewController(IFeedbackManager _feedbackManager, IMembershipManager _membershipManager)
        {
            this._feedbackManager = _feedbackManager;
            this._membershipManager = _membershipManager;
        }

        public ReviewController() : this(new FeedbackManager(), new MembershipManager())
        {
        }

        public ActionResult Index()
        {
            var feedbacks = _feedbackManager.GetUnapprovedReviews();
            var model = new List<ReviewItemViewModel>();

            foreach (var feedback in feedbacks)
            {
                model.Add(new ReviewItemViewModel() {
                    CommenterName = feedback.UserId.HasValue ? _membershipManager.GetUserNameById(feedback.UserId.Value) : feedback.FullName,
                    
                    CommentText = feedback.Comment,
                    CreateDate = feedback.CreateDate,
                    Id = feedback.Id
                });
            }

            return View(model);
        }

        public ActionResult DeleteReview(long id)
        {
            var deleted = _feedbackManager.DeleteReview(id);
            if (deleted)
            {
                Info = String.Format("Review with ID {0} has been successfully deleted", id);
            }
            else
            {
                Error = String.Format("Review with ID {0} deletion failed", id);
            }
            return RedirectToAction("Index");

        }

        public ActionResult ApproveReview(long id)
        {
            var approved = _feedbackManager.ApproveReview(id);
            if (approved)
            {
                Info = String.Format("Review with ID {0} has been successfully approved", id);
            }
            else
            {
                Error = String.Format("Review with ID {0} approving failed", id);
            }
            return RedirectToAction("Index");

        }

    }
}
